package controller

import (
	"context"
	pb "gitee.com/micro-services/protocol/user/api"
	upb "gitee.com/micro-services/protocol/user/server"
	"log"
	"micro.user.client/ao"
	"micro.user.client/integration"
	"micro.user.client/plugins"
)

type UserController struct{}

func (uc *UserController) CreateUser(ctx context.Context, r *pb.APICreateUserReq, w *pb.APICreateUserRsp) error {
	ao.CreateUser(ctx, r, w)
	return nil
}

func (uc *UserController) QueryUserInfo(ctx context.Context, r *pb.APIQueryUserInfoReq, w *pb.APIQueryUserInfoRsp) error {
	log.Printf("request:%+v", r)

	span, md, spanCtx := plugins.TracerContext(ctx, "QueryUserInfo")

	rsp, err := integration.GetProxyManger().UserServiceProxy.QueryUserInfo(spanCtx, &upb.QueryUserInfoReq{
		HotelID:    r.HotelID,
		UserIDList: r.UserIDList,
	})
	if err != nil {
		log.Printf("error:%+v", err)
		plugins.TracerErrorInfo(span, "go.micro.api.user.QueryUserInfo", md, r, w, rsp, err)
		return err
	}

	w.Info = rsp.Info

	plugins.TracerFinish(span, "go.micro.api.user.QueryUserInfo", md, r, w)
	return nil
}

func (uc *UserController) UpdateUserInfo(ctx context.Context, r *pb.APIUpdateUserInfoReq, w *pb.APIUpdateUserInfoRsp) error {
	log.Printf("request:%+v", r)

	span, md, spanCtx := plugins.TracerContext(ctx, "UpdateUserInfo")

	rsp, err := integration.GetProxyManger().UserServiceProxy.UpdateUserInfo(spanCtx, &upb.UpdateUserInfoReq{
		UserID:   r.UserID,
		UserName: r.UserName,
		PassWord: r.PassWord,
		Phone:    r.Phone,
		Email:    r.Email,
		Role:     r.Role,
		HotelID:  r.HotelID,
	})
	if err != nil {
		log.Printf("error:%+v", err)
		plugins.TracerErrorInfo(span, "go.micro.api.user.UpdateUserInfo", md, r, w, rsp, err)
		return err
	}

	w.RetCode = rsp.RetCode
	w.Msg = rsp.Msg

	plugins.TracerFinish(span, "go.micro.api.user.UpdateUserInfo", md, r, w)
	return nil
}

func (uc *UserController) DeleteUser(ctx context.Context, r *pb.APIDeleteUserReq, w *pb.APIDeleteUserRsp) error {
	log.Printf("request:%+v", r)

	span, md, spanCtx := plugins.TracerContext(ctx, "DeleteUser")

	rsp, err := integration.GetProxyManger().UserServiceProxy.DeleteUser(spanCtx, &upb.DeleteUserReq{
		UserIDList: r.UserIDList,
	})
	if err != nil {
		log.Printf("error:%+v", err)
		plugins.TracerErrorInfo(span, "go.micro.api.user.DeleteUser", md, r, w, rsp, err)
		return err
	}

	w.RetCode = rsp.RetCode
	w.Msg = rsp.Msg

	plugins.TracerFinish(span, "go.micro.api.user.DeleteUser", md, r, w)
	return nil
}
